package com.hualan.service;

import com.hualan.domain.User;


import com.hualan.mapper.UserMapper;
import com.hualan.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;


    //    登录
//    因数据库表的字段为username不为一，因此在检索到多个user会报错，将返回值限定为1个，但是后续要修改字段属性为唯一
    public int login(String name,String passwd){

        User user = userMapper.findUserByUsername(name);
        if(user==null){
            return 0;
        }else if(user.getPassword().equals(passwd)){
            return 1;
        }else {
            return -1;
        }
    }
    public int checkphone (String phone){
        int checkphone = userMapper.checkphone(phone);
        if (checkphone != 1){
            return 1;
        }else return -1;
    }


    public int insertall(String username, String passwd,String phone) {

        User user = new User();
        user.setUsername(username);
        user.setPassword(passwd);
        user.setPhone(phone);
        user.setRole(3);

        int i = userMapper.selectByUsername(username);
        if (i == 1){
            return -2;
        }else {
            int insert = userMapper.insert(user);
            if (insert == 1){
                return 1;
            }else return -1;
        }

    }



    public String selectphonebyusername(String username) {
        return userMapper.selectphonebyusername(username);
    }

    public int updatepasswdbyusername(String username, String password) {
        int i = userMapper.updatepasswdbyusername(username, password);
        if (i == 1){
            return 1;
        }
        return -1;

    }
    public R selectUserByName(String name){
        User user=userMapper.findUserByUsername(name);
        if (user==null){
            return R.fail(500);
        }else {
            return R.success(user);
        }
    }


//    vue登录
    public int findAdUserByUsername(String username, String password) {
        //登录业务
        User user = userMapper.findAdUserByUsername(username);

        //登录逻辑判断
        if (user==null){
            return 0;
        }else if (user.getPassword().equals(password)){
            //管理员登录
            if (user.getRole() == 1){
                return 11;
            }
            //咨询师登录
            if (user.getRole() == 2){
                return 21;
            }else {
                //用户登录
                return 30;
            }
        }else{
            return 2;
        }
    }
}
